﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;

namespace ArachiLib
{
    public class WebPage
    {
        Uri _siteLocation;

        /// <summary>
        /// The location where this page resides.
        /// </summary>
        public Uri SiteLocation
        {
            get { return _siteLocation; }
            set { _siteLocation = value; }
        }

        URIReference[] _references;

        /// <summary>
        /// The collection of references that this page contains.
        /// </summary>
        public URIReference[] References
        {
            get { return _references; }
            set { _references = value; }
        }

        string _pageData;

        /// <summary>
        /// The web page's content.
        /// </summary>
        public string PageData
        {
            get { return _pageData; }
            set { _pageData = value; }
        }



        public void ParseMarkupForRefs(string markup)
        {
            //<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>
            List<URIReference> list = new List<URIReference>();
            try
            {
                Match m = null;
                if((m = Regex.Match(markup, "<a\\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)</a>")).Success)
                {
                    while ((m.NextMatch()).Success)
                    {
                        string s = m.Value;
                       
                    }
                }
                

            }
            catch(Exception e)
            {
                Console.Error.WriteLine("Test Failed: {0}", e.Message);
            }
        }

        private void GetRefsFromIterator(ref List<URIReference> refs, Match m)
        {
            string s = "";

            while (m.NextMatch().Success)
            {
                //TODOTODOTODO
                if (!(s.EndsWith("css") || s.EndsWith("ico") || s[0] == '#'))
                {
                    REFTYPE refType = REFTYPE.UNDEFINED;
                    Uri uSite = null;
                    if (s.StartsWith("http://"))
                    {
                        if (s.Contains(SiteLocation.ToString()))
                        {
                            refType = REFTYPE.INTERNAL;
                        }
                        else
                        {
                            refType = REFTYPE.EXTERNAL;
                        }
                        uSite = new Uri(s);
                    }
                    else
                    {
                        refType = REFTYPE.INTERNAL_REF;
                        if (s[0] == '/')
                        {
                            uSite = new Uri(new Uri(SiteLocation.Host), s);
                        }
                        else
                        {
                            uSite = new Uri(SiteLocation, s);
                        }
                    }
                    if (uSite != null && refType != REFTYPE.UNDEFINED)
                    {
                        URIReference uTemp;
                        uTemp.siteLocation = uSite;
                        uTemp.refType = refType;
                        refs.Add(uTemp);
                    }
                }
            }
        }
    }
}

public enum REFTYPE
{
    EXTERNAL,
    INTERNAL,
    INTERNAL_REF,
    UNDEFINED
};

public struct URIReference
{
    public Uri siteLocation;
    public REFTYPE refType;
}